<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="类型选择">
              <el-select v-model="form.type" placeholder="请选择" clearable>
                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-if="hasPermission('AdminMaterialTypeProvider.addMaterialType')" @click="toRoutes('materialTypeAdd')">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="名称" />
        <el-table-column label="图标">
          <template slot-scope="scope">
            <div>
              <img style="height:40px;width:40px" :src="scope.row.img | imgUrlFn" alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
       <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.status == 0 ? '正常' : '下架' }}
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="createdAt" label="添加时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="hasPermission('AdminMaterialTypeProvider.updateMaterialType')" type="text" size="small" @click="toRoutes('materialTypeEdit',scope.row.id)">编辑</el-button>
            <el-button v-if="hasPermission('AdminMaterialTypeProvider.deleteMaterialTypeById')" type="text" size="small" @click="deleteList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination :current-page="listForm.pageNum" :page-sizes="page_sizes" :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper" :total="page_totals" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { materialTypeList, materialTypeListDelete } from "@/api/materialType";
import { policy } from "@/api/policy";
export default {
  name: "MaterialTypeIndex",
  filters: {
    imgUrlFn: function(e) {
      return localStorage.getItem("imgUrlPrefix") + e;
    }
  },
  data() {
    return {
      dates: "",
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: "",
        id: null,
        img: "",
        status: null,
        sort: null
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      typeList: [
        { id: null, name: "全部" },
        { id: 1, name: "手术前后对比照" },
        { id: 2, name: "美咖资料" },
        { id: 3, name: "合作意愿介绍" }
      ]
    };
  },
  mounted() {
    this.getList(this.listForm);
  },
  methods: {
    // 按钮权限
    hasPermission(e){
        return window.helper.hasPermission(e)
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      this.getList(this.form);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      if (datas.type == "") {
        datas.type = null;
      }
      materialTypeList(datas)
        .then(res => {
          if (!res.data.code) {
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
            this.tableData.forEach(item => {
              this.typeList.forEach(list => {
                if (item.type == list.id) {
                  item.type = list.name;
                }
              });
            });
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 路由跳转
    toRoutes(e, userId) {
      const route = { name: e, query: { id: userId } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    // 删除
    deleteList(e) {
      const datas = { id: e };
      console.log(datas);
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(datas);
          materialTypeListDelete(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.getList(this.form);
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
